package com.gs.api.util.video.parse.zuiyou;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.gs.api.util.http.HttpClientUtil;
import com.gs.api.util.video.parse.ParseConstant;
import com.gs.api.util.video.parse.vo.VideoVo;
import okhttp3.*;
import org.jsoup.Jsoup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author guoyunfei on 2022/4/14 0:38
 * @className ZuiyouApp
 * @description TODO 最右
 * @Version 1.0
 */
public class ZuiyouApp {


    public static VideoVo zuiyouParseUrl(String url){

        VideoVo videoVo = new VideoVo();
        try {
            Matcher matcher = Pattern.compile("\\d{9}").matcher(url);
            if(matcher.find()) {
                String pid=matcher.group(0);
                url="https://share.izuiyou.com/api/post/detail";

                OkHttpClient okHttpClient = new OkHttpClient();
                RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{\"pid\":"+pid+"}");
                Request request = new Request.Builder().url(url).post(body).build();
                Response response = okHttpClient.newCall(request).execute();
                String result=response.body().string();
                System.out.println(result);

                JSONObject data = JSONObject.parseObject(result).getJSONObject("data");
                String id = data.getJSONObject("post").getJSONArray("imgs").getJSONObject(0).getString("id");

                videoVo.setCopywriting(data.getJSONObject("post").getString("content"));

                if(null != data.getJSONObject("post").getJSONObject("videos")){
                    videoVo.setVideoSrc(data.getJSONObject("post").getJSONObject("videos").getJSONObject(id).getString("url"));
                    String cover_url = data.getJSONObject("post").getJSONObject("videos").getJSONObject(id).getJSONArray("cover_urls").getString(0);
                    videoVo.setCoverSrc(cover_url);
                    String audioSrc = data.getJSONObject("post").getJSONObject("videos").getJSONObject(id).getJSONArray("h265").getJSONObject(0).getJSONArray("urls").getJSONObject(0).getString("url");
                    videoVo.setAudioSrc(audioSrc);
                    videoVo.setType(ParseConstant.PARSE_VIDEO);
                }else{
                    videoVo.setType(ParseConstant.PARSE_IMAGES);
                    JSONArray imgs = data.getJSONObject("post").getJSONArray("imgs");
                    List<String> list = new ArrayList<>();
                    for (int i = 0; i < imgs.size(); i++) {
                        String imgSrc = imgs.getJSONObject(i).getJSONObject("urls").getJSONObject("origin").getJSONArray("urls").getString(0);
                        list.add(imgSrc);
                        videoVo.setImages(list);
                    }

                }

            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return videoVo;
    }



    public static void main(String[] args) {
        // System.out.println(new ZuiyouApp().zuiyouParseUrl("https://h5.izuiyou.com/detail/265526043?zy_to=applink&to=applink"));
        System.out.println(new ZuiyouApp().zuiyouParseUrl("#最右#分享一条有趣的内容给你，不好看算我输。请戳链接>>https://share.xiaochuankeji.cn/hybrid/share/post?pid=265526043&zy_to=applink&share_count=1&m=a7805458c8339c75aafad414f17cd476&d=4d5b40240535f8fc10a2fdcfc9f392c12c8a047ac50bafd9a779e0e5997bb1b2958a035614d8f6d4163c949d54848d79&app=zuiyou&recommend=r0&name=n0&title_type=t0"));
    }

}
